package com.jrx.anytxn.investor.service.impl;

import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.investor.dto.loan.LoanAccountInvestorInfoRes;
import com.jrx.anytxn.investor.entity.AmLoanAccountInvestorInfo;
import com.jrx.anytxn.investor.mapper.ext.ExtAmLoanAccountInvestorInfoMapper;
import com.jrx.anytxn.investor.service.ILoanAccountInvestorInfoService;
import com.jrx.anytxn.param.dto.investor.PrInvestorTableRes;
import com.jrx.anytxn.param.service.investor.IPrInvestorTableService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 出资方账户实现类
 *
 * @author 黄秋平
 * @create 2020/6/18
 */
@Service
public class LoanAccountInvestorInfoServiceImpl implements ILoanAccountInvestorInfoService {

    private static final Logger logger = LoggerFactory.getLogger(LoanAccountInvestorInfoServiceImpl.class);

    @Resource
    private ExtAmLoanAccountInvestorInfoMapper extAmLoanAccountInvestorInfoMapper;

    @Resource
    private IPrInvestorTableService prInvestorTableService;

    @Override
    public LoanAccountInvestorInfoRes getLoanAccountByInveLoanIdAndCusId(String tenantId, String customerId,String investorLoanId) throws TxnException {
        LoanAccountInvestorInfoRes res;
        AmLoanAccountInvestorInfo loanAccountInvestorInfo = extAmLoanAccountInvestorInfoMapper.selectByInvestorLoanIdAndCustId(investorLoanId,customerId,tenantId);
        if (loanAccountInvestorInfo == null) {
            logger.error("用户：{}没有订单号：{}的订单账户", customerId, investorLoanId);
            throw new TxnBizException(TxnRespCode.NO_DATA.getCode(), TxnRespCode.NO_DATA.getUserTips());
        }
        res=BeanMapping.copy(loanAccountInvestorInfo,LoanAccountInvestorInfoRes.class);
        //获取出资方信息
        PrInvestorTableRes prInvestorTableRes=prInvestorTableService.findByTenIdAndInvestorId(tenantId,loanAccountInvestorInfo.getInvestorId());
        res.setChineseName(prInvestorTableRes.getChineseName());
        return res;
    }

    @Override
    public List<LoanAccountInvestorInfoRes> getAccountByTenIdAndCusIdAndLoanIdAndInveId(String tenantId, String customerId, String loanId, String investorId) throws TxnBizException {
        List<AmLoanAccountInvestorInfo> loanAccountInvestorInfoList=extAmLoanAccountInvestorInfoMapper.selectAccountByTenIdAndCusIdAndLoanIdAndInveId(tenantId, customerId, loanId, investorId);
        return BeanMapping.copyList(loanAccountInvestorInfoList,LoanAccountInvestorInfoRes.class);
    }
}
